/* AnnouncementCustomerController.java
 *
 * Copyright (C) 2013 Universidad de Sevilla
 * 
 * The use of this project is hereby constrained to the conditions of the 
 * TDG Licence, a copy of which you may download from 
 * http://www.tdg-seville.info/License.html
 * 
 */

package controllers.customer;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.AnnouncementService;
import services.CustomerService;
import services.RegistrationService;

import controllers.AbstractController;
import domain.Announcement;

@Controller
@Transactional
@RequestMapping("/announcement/customer")
public class AnnouncementCustomerController extends AbstractController {
	
	// Services ---------------------------------------------------------------
	@Autowired
	AnnouncementService announcementService;
	// TODO: Add autowired services. 
	@Autowired
	RegistrationService registrationService;
	@Autowired
	CustomerService customerService;
	// Constructors -----------------------------------------------------------
	
	public AnnouncementCustomerController() {
		super();
	}

	// Listing ----------------------------------------------------------------
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list() {
		ModelAndView result;
		// TODO: Complete this method.
        Collection<Announcement> announcements;
        Collection<Announcement> registeredAnnouncements;
        announcements = announcementService.findAll();
        registeredAnnouncements = announcementService.findRegistered();
        result = new ModelAndView("announcement/list");
        result.addObject("announcements", announcements);
        result.addObject("registeredAnnouncements", registeredAnnouncements);
        result.addObject("requestURI", "announcement/customer/list.do");
        
        return result;
    }

	// Registration -----------------------------------------------------------
	
	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public ModelAndView register(@RequestParam int announcementId) {
		ModelAndView result;		
				
		// TODO: Complete this method.
		try {
			beginTransaction();	
			customerService.registerPrincipal(announcementId);
			commitTransaction();	
			result = this.list();
			result.addObject("message", "announcement.commit.ok");
		} catch (Throwable oops) {
			rollbackTransaction();
			result = this.list();
			result.addObject("message", "announcement.commit.error");
		}
		
		return result;
	}
		
	@RequestMapping(value = "/unregister", method = RequestMethod.GET)
	public ModelAndView unregister(@RequestParam int announcementId) {
		ModelAndView result;		
				
		// TODO: Complete this method.
		try {
			beginTransaction();	
			customerService.unregisterPrincipal(announcementId);
			commitTransaction();	
			result = this.list();
			result.addObject("message", "announcement.commit.ok");
		} catch (Throwable oops) {
			rollbackTransaction();
			System.out.println(oops.getMessage());
			result = this.list();
			result.addObject("message", "announcement.commit.error");
		}
		
		return result;
	}
}